package net.hn.hnms.biz.cockpit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.hn.hnms.biz.cockpit.domain.YearHiddenTrouble;
import net.hn.hnms.biz.cockpit.domain.vo.YearHiddenTroubleVo;
import net.hn.hnms.biz.cockpit.mapper.YearHiddenTroubleMapper;
import net.hn.hnms.biz.cockpit.service.IYearHiddenTroubleService;
import net.hn.hnms.biz.cockpit.utils.CalculateUtil;
import net.hn.hnms.sys.common.core.utils.DateUtils;
import org.springframework.stereotype.Service;

@Service
public class YearHiddenTroubleServiceImpl extends ServiceImpl<YearHiddenTroubleMapper, YearHiddenTrouble> implements IYearHiddenTroubleService {

    @Override
    public YearHiddenTroubleVo getYearDanger(String mineCode) {
        YearHiddenTroubleVo result = new YearHiddenTroubleVo();
        String year = DateUtils.dateTimeNow(DateUtils.YYYY);
        YearHiddenTrouble yearHiddenTrouble = baseMapper.selectOne(new LambdaQueryWrapper<YearHiddenTrouble>().eq(YearHiddenTrouble::getMineCode, mineCode)
                .eq(YearHiddenTrouble::getCheckPeriod, year).last("limit 1"));
        if (yearHiddenTrouble != null) {
            result.setTotal(Integer.parseInt(yearHiddenTrouble.getDangersCount()))
                    .setRectifyCount(Integer.parseInt(yearHiddenTrouble.getRectifyCount()))
                    .setRectifyRate(CalculateUtil.getPercent(result.getTotal().toString(), result.getRectifyCount().toString()))
                    .setTimeoutCount(Integer.parseInt(yearHiddenTrouble.getTimeoutCount()))
                    .setTimeoutRate(CalculateUtil.getPercent(result.getTotal().toString(), yearHiddenTrouble.getTimeoutCount()))
                    .setMajorDanger(Integer.parseInt(yearHiddenTrouble.getMajorDanger()))
                    .setMajorDangerRate(CalculateUtil.getPercent(result.getTotal().toString(), yearHiddenTrouble.getMajorDanger()));
        }
        return result;
    }

}
